﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlClient;
namespace PageDisplay
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //LoadData();
            }
        }

        public void LoadData()
        {
            string sql = @"select * from ( select tempid=ROW_NUMBER() over(order by id),* from class1) x where tempid>=@start and tempid<=@end ";
            string sql1 = "select count(*) from class1";
            
            pager.RecordCount = Convert.ToInt32(DBAccess.ExecuteScalar(sql1));
            SqlParameter[] ps = { 
                                    new SqlParameter("@start",pager.StartRecordIndex),
                                    new SqlParameter("@end",pager.EndRecordIndex)
                                };
            DataTable dt= DBAccess.ExecuteDataTable(sql, ps);

            GridView1.DataSource = dt;
            GridView1.DataBind();

        }

        protected void pager_PageChanged(object sender, EventArgs e)
        {
            LoadData();
            //Label1.Text = pager.CurrentPageIndex.ToString();
        }


        protected void btDelete_Click(object sender, EventArgs e)
        {
            List<int> listId = new List<int>();
            int rCount = GridView1.Rows.Count;
            for (int i = 0; i < rCount; i++)
            {
                if (((CheckBox)(GridView1.Rows[i].Cells[0].FindControl("cbItem"))).Checked == true)
                {
                    listId.Add(Convert.ToInt32( GridView1.Rows[i].Cells[2].Text));
                }
            }
            string sql = "delete from class1 where id=@id";
            foreach (int item in listId)
            {
                SqlParameter ps = new SqlParameter("@id",item);
                DBAccess.ExecuteNonQuery(sql, ps);
            }
        }


        protected void SelectAll(object sender, EventArgs e)
        {
            bool isChecked = ((CheckBox)(GridView1.HeaderRow.Cells[0].FindControl("cbHead"))).Checked;
            foreach (GridViewRow gvRow in GridView1.Rows)
            {
                ((CheckBox)(gvRow.Cells[0].FindControl("cbItem"))).Checked = isChecked;
            }
        }
    }
}